#!/usr/bin/env python
# -*- coding: utf-8 -*-

from WSException import WSException
from WSOption import WSOption
import time

class WSModule():
    log_path = None
    log_width = 60 # TODO ширина лога
    description = "module description"
    result = None
    options = []
    def __init__(self, kernel):
        self.kernel = kernel
        if self.log_path == None:
            raise WSException('Module must have log path!')

    def run(self):
        pass

    def help(self):
        pass

    def finished(self):
        pass

    def getResult(self):
        def write_options(fh, options, parent_name):
            for option in options:
                if isinstance(options[option], WSOption):
                    fh.write('| {0: <36} |'.format(parent_name + options[option].name + ": "
                                                             + str(options[option].value)) + "\n")
                elif isinstance(options[option], dict):
                    write_options(fh, options[option], parent_name + option + '.')
        fh = open(self.log_path, 'a')
        fh.write("" + '{:=^40}'.format('') + "\n")
        fh.write('|{0: ^38}|'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + "\n")

        fh.write('|{0: ^38}|'.format("Work options:") + "\n")
        write_options(fh, self.options, '')
        fh.write('{:=^40}'.format('') + "\n")

        if self.result:
            fh.write(self.result.as_string())
        else:
            fh.write("None")
        fh.write("\n")

        fh.close()

        return "Writed in " + self.log_path
